// VBConversions Note: VB project level imports
using System.Data;
using DevExpress.XtraEditors.Controls;
using System.Diagnostics;
using System.Xml.Linq;
using DevExpress.XtraEditors.Repository;
using System.Collections.Generic;
using System.Data.SqlClient;
using Microsoft.VisualBasic;
using System.Configuration;
using System.Collections;
using DevExpress.XtraEditors;
using System.Data.Sql;
using Microsoft.SqlServer;
using System.Windows.Forms;
using System;
using DevExpress.XtraReports;
using Microsoft.SqlServer.Management.Smo;
using DevExpress;
using System.Linq;
using System.IO;
// End of VB project level imports


namespace SCIEF.SysBFW.CommonExt
{
	public class MetaString
	{
#region  Property Field
		//Private _stringIndexID As Guid
		
		public string DefaultString {get; set;}
		private string _DefaultText = null;
		public string DefaultText
		{
			get
			{
				return _DefaultText;
			}
			set
			{
				_DefaultText = value;
			}
		}
		private Guid _LanguageStringID = Guid.Empty;
		public Guid LanguageStringID
		{
			get
			{
				return _LanguageStringID;
			}
			set
			{
				_LanguageStringID = value;
			}
		}
		public string StringCd {get; set;}
		public Guid StringID {get; set;}
		public int StringTypeID {get; set;}
		private Guid _TextFormatID = Guid.Empty;
		public Guid TextFormatID
		{
			get
			{
				return _TextFormatID;
			}
			set
			{
				_TextFormatID = value;
			}
		}
		
		/// <summary>
		///
		/// </summary>
		/// <param name="sqlString"></param>
		/// <returns></returns>
		/// <remarks></remarks>
		public static MetaString GetMetaString(string sqlString)
		{
			
			SqlCommand command = Base.Connection.Command();
			SqlDataReader reader = null;
			DataTable dtrow = new DataTable();
			DataRow dr = null;
			//
			MetaString ms = null;
			//
			try
			{
				command.CommandText = sqlString;
				reader = command.ExecuteReader(CommandBehavior.CloseConnection);
				if (reader.HasRows)
				{
					dtrow.Load(reader);
					dr = dtrow.Rows[0];
					ms = new MetaString();
					//Pk always there
					ms.StringID = (Guid) (dr["StringID"]);
					ms.StringCd = dr["StringCd"].ToString();
					ms.DefaultString = dr["String"].ToString();
					if (!Microsoft.VisualBasic.Information.IsDBNull(dr["Text"]))
					{
						ms.DefaultText = dr["Text"].ToString();
					}
					ms.StringTypeID = System.Convert.ToInt32(dr["StringTypeID_FK"]);
					ms.TextFormatID = (Guid) (dr["TextFormatID_FK"]);
					if (!Microsoft.VisualBasic.Information.IsDBNull(dr["LanguageStringID_FK"]))
					{
						ms.LanguageStringID = new Guid(dr["LanguageStringID_FK"].ToString());
					}
				}
			}
			catch (Exception)
			{
				throw (new Exception(string.Format("Invalid SQL command: MetaString.GetMetaStringByCode{0} {1}", "\r\n", command.CommandText)));
			}
			finally
			{
				if (reader != null)
				{
					reader.Close();
				}
				dtrow.Dispose();
				command.Dispose();
			}
			return ms;
		}
		
		public static MetaString GetMetaStringByID(Guid id, Guid? languageID = null)
		{
			const string selectStatement = "SELECT MS.StringID, MS.StringCd, COALESCE(MSL.StringLocal, MS.DefaultString) as String , COALESCE(MSL.TextLocal, MS.DefaultText) as Text, MS.StringTypeID_FK, MS.TextFormatID_FK, MSL.LanguageStringID_FK FROM MetaStrings AS MS LEFT JOIN MetaStringLocals AS MSL ON MS.StringID = MSL.StringID_FK AND MSL.LanguageStringID_FK = \'{1}\' WHERE (MS.StringID = \'{0}\')";
			if (string.IsNullOrEmpty(languageID.ToString()))
			{
				languageID = Guid.Empty;
			}
			return GetMetaString(string.Format(selectStatement, id.ToString(), languageID.ToString()));
		}
		
		public static MetaString GetMetaStringByCode(string code, Guid? languageID = null)
		{
			
			const string selectStatement = "SELECT MS.StringID, MS.StringCd, COALESCE(MSL.StringLocal, MS.DefaultString) as String , COALESCE(MSL.TextLocal, MS.DefaultText) as Text, MS.StringTypeID_FK, MS.TextFormatID_FK, MSL.LanguageStringID_FK FROM MetaStrings AS MS LEFT JOIN MetaStringLocals AS MSL ON MS.StringID = MSL.StringID_FK AND MSL.LanguageStringID_FK = \'{1}\' WHERE (MS.StringCd = \'{0}\')";
			if (string.IsNullOrEmpty(languageID.ToString()))
			{
				languageID = Guid.Empty;
			}
			return GetMetaString(string.Format(selectStatement, code.ToString(), languageID.ToString()));
			
			//Dim command As SqlCommand = Base.Connection.Command
			//Dim reader As SqlDataReader = Nothing
			//Dim dtrow As DataTable = New DataTable
			//Dim dr As DataRow = Nothing
			//'
			//Dim ms As MetaString = Nothing
			//If String.IsNullOrEmpty(languageID.ToString) Then
			//    languageID = Guid.Empty
			//End If
			//Try
			//    command.CommandText = String.Format(selectStatement, code, languageID.ToString)
			//    reader = command.ExecuteReader(CommandBehavior.CloseConnection)
			//    If reader.HasRows Then
			//        dtrow.Load(reader)
			//        dr = dtrow.Rows(0)
			//        ms = New MetaString
			//        'Pk always there
			//        ms.StringID = CType(dr("StringID"), Guid)
			//        ms.StringCd = dr("StringCd").ToString
			//        ms.DefaultString = dr("String").ToString
			//        If Not IsDBNull(dr("Text")) Then
			//            ms.DefaultText = dr("Text").ToString
			//        End If
			//        ms.StringTypeID = CInt(dr("StringTypeID_FK"))
			//        ms.TextFormatID = CType(dr("TextFormatID_FK"), Guid)
			//        If Not IsDBNull(dr("LanguageStringID_FK")) Then
			//            ms.LanguageStringID = New Guid(dr("LanguageStringID_FK").ToString)
			//        End If
			//    End If
			//Catch ex As Exception
			//    Throw New Exception(String.Format("Invalid SQL command: MetaString.GetMetaStringByCode{0} {1}", vbCrLf, command.CommandText))
			//Finally
			//    If reader IsNot Nothing Then
			//        reader.Close()
			//    End If
			//    dtrow.Dispose()
			//    command.Dispose()
			//End Try
			//Return ms
		}
#endregion
	}
	
}
